package newx.utils
{
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.KeyboardEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
    /**
     * 组合键判断管理
     */
	public class KeyCommand extends EventDispatcher
    {        
        
//        public static var KONAMI_COMMAND:Array =  [
//            {code:38},
//            {code:38},
//            {code:40},
//            {code:40},
//            {code:37},
//            {code:39},
//            {code:37},
//            {code:39},
//            {code:66},
//            {code:65}
//
//        ]; // 上上下下左右左右BA

        private var _command:Array;
        private var _timeout:Number;
        private var _stage:Stage;
        private var _keyHistory:Array;
        private var _timeoutTimer:Timer;
		private var _callBackFunc:Function;
		
		/**
		 * 新建组合键管理者
		 * @param command		：键组
		 * @param stage			：用于监听的显示对象
		 * @param callBackFunc	：触发回调函数
		 * @param timeout		：键间隔（毫秒）
		 * @return 
		 * 
		 */
		public function KeyCommand(
			command			:Array,
			stage			:Stage,
			callBackFunc	:Function,
			timeout			:Number = 2000 )
        {
            _command 		= 	command;
			_callBackFunc	=	callBackFunc;
            _timeout 		= 	timeout;
            _keyHistory		= 	[];
			
            _stage = stage;
            _stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDwonHandler);
            _timeoutTimer = new Timer(_timeout);
            _timeoutTimer.addEventListener(TimerEvent.TIMER, timerHandler);
		}
        

        private function keyDwonHandler(e:KeyboardEvent):void
        {
            if (_timeoutTimer != null) _timeoutTimer.reset();
            if (!_timeoutTimer.running) _timeoutTimer.start();
            if (e.keyCode == 16) return; // 16 = shift
			
            _keyHistory.push(e.keyCode);

            if (_keyHistory.length > _command.length) _keyHistory.shift();

            var flag:Boolean = true;
            var i:int = 0;
            var l:int = _command.length;

            for (i; i<l; i++)
            {
                if (_keyHistory[i] != _command[i].code)
                {
                    flag = false;
                    break;
                } else {
                    if(_command[i].shift)
                    {
                        if (!e.shiftKey)
                        {
                            flag = false;
                            break;
                        }
                    }
                }
            }

            if (flag) commandSuccess();
        }


        private function commandSuccess():void
        {
            dispatchEvent(new Event(Event.COMPLETE));
			_callBackFunc.call(this);			
			timerHandler(null);
        }

        private function timerHandler(e:TimerEvent):void
        {
			_stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDwonHandler);
			_timeoutTimer.stop();
			_timeoutTimer.removeEventListener(TimerEvent.TIMER, timerHandler);
			
            _keyHistory 	= 	null;
			_timeoutTimer	=	null;
        }

        
		override public function toString():String
        {
			return "[KeyCommand] command :" + _command;
		}
	}
}
